<?php
	include 'redirectUser.php';
//////////////////////////////////////
	$servicePerPage = 10;
	$user = $_SESSION['curUser'];
//////////////////////////////////////

	$userServiceControls = new UserServiceSearchControls();
	$userServiceControls->setUserId($user->getId());
	
	$userservices = UserServiceUtils::find($userServiceControls);
//////////////////////////////////////
	
	$page = null;
	if(! (($page = ParamUtils::get("paginator")) != null && VariableUtils::isNumber($page))){
		$page = 1;		 
	}
	
	$usid = null;
	$action = null;
	if(($action = ParamUtils::get("action")) == null){
		$action = "show";
	}
?>




<?php if($action == "show") { ?>
	<div style="position: absolute; left: 30px; top: 10px;">
	<table>
		<tr><th>Id</th><th>Nazwa</th><th>Opis</th><th>Od</th><th>Do</th><th></th></tr>
		<?php 
			for($i = ($page - 1) * $servicePerPage ; $i < $page * $servicePerPage && $i < sizeof($userservices); $i++){
				$userservice = $userservices[$i];
				$warn = PaymentUtils::hasPaymentWarn($userservice->getId()) ?  "Ostrzezenie o zaplacie" : "";
				echo "<tr> 
							<td>
							" .  $userservice->getId() . 
							"
							</td>
							<td>
							" .  $userservice->getName() . 
							"
							</td>
							<td>
							" .  $userservice->getDesc() . 
							"
							</td>
							<td>
							" .  $userservice->getDateFrom(). 
							"
							</td>
							<td>
							" .  $userservice->getDateTo() . 
							"
							</td>
							<td>
								<a href='bookmarks.php?page=myservice&action=manage&usid=".$userservice->getId()."'>Zobacz</a>						
							</td>
								
							<td class='warn'>
								$warn
							</td>
				
					 </tr>";
				
			}
			
			$pages = ceil(sizeof($userservices) / $servicePerPage);
			if($pages > 1){
				echo "<tr><td colspan='3'>";
				for($i = 1 ; $i <= $pages ; $i++){
					echo "<a href='bookmarks.php?page=myservice&paginator=$i'> |$i| </a>";	
				}
				echo "</td></tr>";
			}
		?>	
	
	
	</table>
	</div>
	
<?php } else {
// MANAGE USER SERVICE

	$userservice  = null;
	if(ParamUtils::get("usid") != null){
		$userservice = UserServiceUtils::get(ParamUtils::get("usid"));
		
		$desc= null;
		if(ParamUtils::get("send") != null && "add" == ParamUtils::get("subaction") && ($desc = ParamUtils::get("desc")) != null){
				$newTicket = TicketUtils::createTicket($userservice->getId(), $usid, $desc);
				TicketUtils::add($newTicket);
		}
		
		$ctid = null;
		if("close" == ParamUtils::get("subaction") && ($ctid = ParamUtils::get("ctid")) != null){
			TicketUtils::unActive($ctid);
		}
	}
	
	if($userservice != null){
		
		$tc = new TicketSearchControls();
		$tc->setUserId($user->getId());
		$tc->setActive(true);
		$tc->setUserServiceId($userservice->getId());
		$tickets = TicketUtils::find($tc);
		
?>

	<div><?php echo $userservice->getName(); ?></div>
	<div style="margin-left: 34px;">
	<table>
		<tr>
			<td>
				<?php if(sizeof($tickets) > 0) { ?>
				<table>
					<caption>ZGŁOSZENIA</caption>
					<tr>
						<th>Opis</th>
						<th>Ilosc odpowiedzi</th>
						<th>&nbsp;</th>
						<th>&nbsp;</th>
					</tr>
					
					<?php foreach($tickets as $ticket){
							$descs = $ticket->getDescriptions();	
							if(sizeof($descs)==0){
								TicketUtils::remove($ticket);	
							}
							
							$nbrOfAnswer = 0;
							foreach($descs as $desc){
								if($desc->getUserId() != $user->getId()){
									$nbrOfAnswer++;
								}
							}
							echo "<tr> 
									<td> " . $descs[0]->get() . "</td>
									<td>" . $nbrOfAnswer . "</td>
									<td><a href='bookmarks.php?page=myservice&action=manage&tid=" . $ticket->getId() . "'>Zobacz</a></td>
									<td><a href='bookmarks.php?page=myservice&action=manage&usid=" . $userservice->getId()  . "&subaction=close&ctid=" . $ticket->getId() . "'>Zamknij</a></td> 
								</tr>"; 
						}?>
				</table>
	
				<?php }?>
		
				<form action="bookmarks.php?page=myservice&action=manage&subaction=add&usid=<?php echo $userservice->getId();?>" method="post">
					<table>
						<tr> <td colspan="3">Dodaj zgłoszenie</td>
						<tr>
							<td><textarea cols="30" rows="20" name="desc"></textarea></td>
							<td><input type="hidden" name="send" value="true"/> </td> 
							<td>
								<input type="submit" value="Dodaj"/> 
							</td> 
						</tr>
					</table>	
				</form>
			</td>
				
		</tr>
	</table>
			
	</div>
	<div style ="position: absolute; right: -182px; top: 18px;">
				<table>
					<caption>PŁATNOŚCI</caption>
					<tr>
						<th>Część</th>
						<th>Kwota</th>
						<th>Termin zapłaty</th>
						<th>Zapłacono</th>
					</tr>
					
					<?php 
						$payment = PaymentUtils::get($userservice->getId());
						foreach($payment->getParts() as $ppart){
							echo "<tr> 
									<td>". $ppart->getPart() . "</td>
									<td>". $ppart->getValue() . "</td>
									<td>". $ppart->getTillDate() . "</td>
									<td><input type='checkbox' disabled='disabled' ". ($ppart->isPaid() ? "CHECKED" : "") . "/> </td>
								</tr>
							";
						}
					?>
				</table>
				</div>
<?php }

	$ticket = null;
	if(ParamUtils::get("tid") != null){
		$ticket = TicketUtils::get(ParamUtils::get("tid"));
		$desc = null;
		if(ParamUtils::get("tid") != null && "add"==ParamUtils::get("subaction") && ($desc = ParamUtils::get("desc")) != null){
			$newTD = new TicketDescription();
			$newTD->setTid($ticket->getId());
			$newTD->setUserId($user->getId());
			$newTD->set($desc);
			TicketUtils::addDescription($newTD);
			$ticket->add($newTD);
		}
	}

	if($ticket != null){		
		
?>
		
		
<table>
<?php  foreach($ticket->getDescriptions() as $ticketDesc){
		$userWhoWrite = UserUtils::get($ticketDesc->getUserId());
		echo "<tr> 
						<td>". $userWhoWrite->getName() ." " . $userWhoWrite->getSurname() ." </td> 
				 </tr>
				 <tr> 
						<td>" . stripslashes($ticketDesc->get()) . "</td> 
				 </tr>
			";	
		}
		?>
</table>



	<form action="bookmarks.php?page=myservice&action=manage&subaction=add&tid=<?php echo $ticket->getId();?>" method="post">
		<table>
			<tr> <td colspan="3">Odpowiedz</td>
			<tr>
				<td><textarea cols="30" rows="20" name="desc"></textarea></td>
				<td><input type="hidden" name="send" value="true"/> </td> 
				<td>
					<input type="submit" value="Dodaj"/>
					<a href="bookmarks.php?page=myservice&action=manage&usid=<?php echo $ticket->getUsid();?>">Powrót</a>	
				</td> 
			</tr>
		</table>	
	</form>


<?php }?>

<?php
	}?>